package io.renren.modules.sys.customer.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.renren.common.utils.Constant;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.sys.customer.entity.BtCustomerBaseEntity;
import io.renren.modules.sys.customer.entity.BtCustomerNavbarEntity;
import io.renren.modules.sys.customer.service.BtCustomerBaseService;
import io.renren.modules.sys.customer.service.BtCustomerNavbarService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author goudandan
 * @email resjz1@163.com
 * @date 2018-09-15 11:15:28
 */
@RestController
@RequestMapping("sys/btcustomernavbar")
public class BtCustomerNavbarController extends AbstractController {
    @Autowired
    private BtCustomerNavbarService btCustomerNavbarService;
    @Autowired
    private BtCustomerBaseService btCustomerBaseService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:btcustomernavbar:list")
   /* public R list(@RequestParam Map<String, Object> params){
        PageUtils page = btCustomerNavbarService.queryPage(params);

        return R.ok().put("page", page);
    }*/
    public List<BtCustomerNavbarEntity> list(@RequestParam Map<String, Object> params){
        Long uid = getUserId();
        if(uid.equals(0)){
            return null;
        }
        BtCustomerBaseEntity btCustomerBaseEntity = btCustomerBaseService.SelectOneByUid(uid);
        params.put("cust_id",btCustomerBaseEntity.getId());
        List<BtCustomerNavbarEntity> deptList = btCustomerNavbarService.queryList(params);

        return deptList;
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{deptId}")
    @RequiresPermissions("sys:btcustomernavbar:info")
    public R info(@PathVariable("deptId") Long deptId){
        BtCustomerNavbarEntity dept = btCustomerNavbarService.selectById(deptId);

        return R.ok().put("dept", dept);
    }
    @RequestMapping("/info")
    @RequiresPermissions("sys:btcustomernavbar:list")
    public R info(){
        long deptId = 0;
        if(getUserId() != Constant.SUPER_ADMIN){
            Map<String, Object>params = new HashMap<String, Object>();
            BtCustomerBaseEntity btCustomerBaseEntity = btCustomerBaseService.SelectOneByUid(getUserId());
            params.put("cust_id",btCustomerBaseEntity.getId());
            List<BtCustomerNavbarEntity> deptList = btCustomerNavbarService.queryList(params);
            Long parentId = null;
            for(BtCustomerNavbarEntity sysDeptEntity : deptList){
                if(parentId == null){
                    parentId = sysDeptEntity.getPid();
                    continue;
                }

                if(parentId > sysDeptEntity.getPid().longValue()){
                    parentId = sysDeptEntity.getPid();
                }
            }
            deptId = parentId;
        }

        return R.ok().put("id", deptId);
    }
    /**
     * 选择部门(添加、修改菜单)
     */
    @RequestMapping("/select")
    @RequiresPermissions("sys:btcustomernavbar:list")
    public R select(){
        Map<String, Object>params = new HashMap<String, Object>();
        BtCustomerBaseEntity btCustomerBaseEntity = btCustomerBaseService.SelectOneByUid(getUserId());
        params.put("cust_id",btCustomerBaseEntity.getId());
        List<BtCustomerNavbarEntity> deptList = btCustomerNavbarService.queryList(params);
        return R.ok().put("deptList", deptList);
    }
    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:btcustomernavbar:save")
    public R save(@RequestBody BtCustomerNavbarEntity btCustomerNavbar){
        btCustomerNavbarService.insert(btCustomerNavbar);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:btcustomernavbar:update")
    public R update(@RequestBody BtCustomerNavbarEntity btCustomerNavbar){
        ValidatorUtils.validateEntity(btCustomerNavbar);
        btCustomerNavbarService.updateAllColumnById(btCustomerNavbar);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:btcustomernavbar:delete")
    public R delete(long deptId){
        //判断是否有子部门
        List<Long> deptList = btCustomerNavbarService.queryNavBarIdList(deptId);
        if(deptList.size() > 0){
            return R.error("请先删除子部门");
        }
        btCustomerNavbarService.deleteById(deptId);

        return R.ok();
    }
}
